home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / mpfeel.lha / MPFeel / Plurals / mp_ciferio.m < prev    next >
Text File  |  1992-05-12  |  9KB  |  381 lines

  1. /*
  2.  *    MP_Lisp
  3.  *
  4.  *    Author:    S.C.Merrall
  5.  *
  6.  *    File:    mp_cifer.m
  7.  *
  8.  *    Contents:    encode
  9.  *            decode
  10.  *            fe_decode
  11.  *                      next_byte
  12.  *
  13.  *    Description:    These functions allow lisp objects to be easily 
  14.  *            moved between the front end and the backend, and
  15.  *            between processing elements. A lisp structure is
  16.  *            encoded into a character string which can be 
  17.  *            easily transferred, and then this is string is
  18.  *            used as a description to build the object in the
  19.  *            new location
  20.  *
  21.  *    Change History:
  22.  *
  23.  *    Date   Name Comment
  24.  *    -------- ---- -------
  25.  *    25:06:91 SCM  Created - code copied from mp_print.m
  26.  *
  27.  */
  28.  
  29.  
  30. #include <mpl.h>
  31. #include <stdio.h>
  32.  
  33. #include "constant.h"
  34.  
  35. #include "mp_object.h"
  36. #include "mp_debug_off.h"
  37. #include "mp_mem_mgmt.h"
  38. #include "mp_gc.h"
  39. #include "mp_type.h"
  40.  
  41.  
  42. /*----------------------------------------------------------------------------*
  43.  * Function   : encode
  44.  *
  45.  * Parameters : MP_PluralHeap MPPH_obj:    Handle on things to be encoded
  46.  *
  47.  * Description: Encodes arbitrary lisp objects in to character strings in
  48.  *        The processor scratch space. (Makes sure it fits the
  49.  *              scratch space by giving up early if there isn't enough room!
  50.  *              decode will interpret the missing stuff as nils.
  51.  *
  52.  * Result     : int FAIL/SUCCESS
  53.  *---------------------------------------------------------------------------*/
  54.  
  55. #ifdef __STDC__
  56.  
  57. int encode( MP_PluralHeap MPPH_obj );
  58.  
  59. #else
  60.  
  61. int encode( MPPH_obj )
  62.  
  63. MP_PluralHeap MPPH_obj;
  64.  
  65. #endif
  66.  
  67. {
  68.   plural natural *plural general_vector;
  69.   plural int size = MP_LENGTH(MP_CONS_SIZE);
  70.   int result_status = SUCCESS;
  71.   int i;
  72.   float real;
  73.   plural natural tmp;
  74.   MP_PluralHeap MPPH_tmp = &tmp;
  75. DBG_CALL("encode");
  76. DBG_ARGS(fprintf(dbg,"MPPH_obj = ????"));
  77.  
  78.   if (scratch[0] >= (SCRATCH_MEMORY_SIZE - 4)) {
  79.  
  80. DBG_FAIL(fprintf(dbg,"FAIL: Exceeded scratch space"));
  81.     return FAIL;
  82.   }
  83.  
  84.   if ((OA_offsets(MPPH_obj) == NIL) || (OA_offsets(MPPH_obj) == NOT_NIL)) {
  85.  
  86.     scratch[scratch[0]++] = MP_SPECIAL;
  87.     scratch[scratch[0]++] = OA_offsets(MPPH_obj);
  88.   }
  89.   else {
  90.  
  91.     scratch[scratch[0]++] = (plural char) OA_info(MPPH_obj);
  92.  
  93.     switch (OA_info(MPPH_obj)) {
  94.  
  95.     case MP_SYMBOL :
  96.     case INTEGER   :
  97.     case MP_FLOAT  :
  98.     
  99.       for (i=0;i<sizeof(int);i++) 
  100.     scratch[scratch[0]++]= *(OA_data(MPPH_obj)+i);
  101.       break;
  102.  
  103.     case MP_VECTOR:
  104.  
  105.       size = MP_LENGTH(OA_space(MPPH_obj));
  106.       scratch[scratch[0]++] = (plural char) size;
  107.  
  108.     case MP_CONS  :
  109.  
  110.       general_vector = (plural natural *plural) OA_data(MPPH_obj);
  111.  
  112.       for (i=0; (i<size) && (result_status != FAIL); i++) {
  113.  
  114.     OA_offsets(MPPH_tmp) = general_vector[i];
  115.     result_status = encode(MPPH_tmp);
  116.       }
  117.  
  118.       default :
  119.  
  120.     result_status = FAIL;
  121.     }
  122.   }
  123.  
  124. /*   if (result_status == FAIL) {
  125.  * 
  126.  * DBG_FAIL(fprintf(dbg,"FAIL: Out of scratch space or unknown object"));
  127.  *     return FAIL;
  128.  *   }
  129.  */
  130.  
  131. DBG_EXIT(fprintf(dbg,"SUCCESS"));
  132.   return SUCCESS;
  133. }
  134.  
  135. /*----------------------------------------------------------------------------*
  136.  * Function   : next_byte
  137.  *
  138.  * Parameters : void
  139.  *
  140.  * Description: In order to be able to use decode for objects encoded on
  141.  *        the front end, it is necessary to read a description which 
  142.  *        could be more than SCRATCH_MEMORY_SIZE bytes long. Using
  143.  *        next_byte rather than explicitly passing along the scratch
  144.  *        space means that when we run out of description the next 64 
  145.  *        can be read.
  146.  *        If scratch is empty, i.e. scratch[0] when first called then
  147.  *        it is assumed a description is being read from the front
  148.  *        end and it is read from there and a copyIn will be done
  149.  *        to affect initialisation
  150.  *        
  151.  * Result     : char :the next byte
  152.  *---------------------------------------------------------------------------*/
  153.  
  154. plural int  fe_des_length;
  155. plural int  fe_des_index;
  156. char       *fe_des;
  157.  
  158. #ifdef __STDC__
  159.  
  160. plural char next_byte( char *description )
  161.  
  162. #else
  163.  
  164. plural char next_byte( description )
  165.  
  166. char *description;
  167.  
  168. #endif
  169.  
  170. {
  171.   int i;
  172.   int integer = 1;
  173.   char *to_integer = (char *) &integer;
  174.  
  175. DBG_CALL("next_byte");
  176. DBG_ARGS(fprintf(dbg,"decsription=%x",description));
  177.  
  178.   if (description != NULL) {
  179.  
  180.     fe_des    = description;
  181.  
  182.     if (copyIn(fe_des, acu_scratch, SCRATCH_MEMORY_SIZE) != 0) {
  183.  
  184.       for (i=0; i<sizeof(int); i++) acu_scratch[i] = *(to_integer + i);
  185.       acu_scratch[sizeof(int)] = NULL;
  186. DBG_FAIL(fprintf(dbg,"FAIL: Error loading initial segment"));
  187.  
  188.       return (plural char) FAIL;    /* Which is also the same as NULL => nil */
  189.     }
  190.  
  191.     fe_des = fe_des + SCRATCH_MEMORY_SIZE;
  192.     sp_memcpy((plural char *plural) scratch, acu_scratch, SCRATCH_MEMORY_SIZE);
  193.  
  194.     for (i=0; i<sizeof(int); i++) *(to_integer+i) = acu_scratch[i];
  195.  
  196.     fe_des_length = (plural int) integer;
  197.     fe_des_index  = (plural int) sizeof(int);
  198.   }
  199.  
  200.  
  201.   if (fe_des_index < fe_des_length) {
  202.  
  203.     if (fe_des_index % SCRATCH_MEMORY_SIZE == 0) {
  204.  
  205.       if (copyIn(fe_des, acu_scratch, SCRATCH_MEMORY_SIZE) != 0) {
  206.  
  207. DBG_FAIL(fprintf(dbg,"FAIL: Error loading segment %d",
  208.          fe_des_index/SCRATCH_MEMORY_SIZE));
  209.  
  210.         fe_des_length = fe_des_index;
  211.         scratch[fe_des_index] = NULL;
  212.       }
  213.       sp_memcpy((plural char *plural)scratch,acu_scratch,SCRATCH_MEMORY_SIZE);
  214.       fe_des = fe_des + SCRATCH_MEMORY_SIZE;
  215.     }
  216.     return scratch[(fe_des_index++)%SCRATCH_MEMORY_SIZE];
  217.   }
  218.   else {
  219.  
  220. DBG_FAIL(fprintf(dbg,"FAIL: Requested more description than there is"));
  221.     return (plural char) 0;
  222.   }
  223.  
  224. DBG_EXIT(fprintf(dbg,"????"));
  225.   return scratch[(fe_des_index++)%SCRATCH_MEMORY_SIZE];
  226. }
  227.  
  228.  
  229. /*----------------------------------------------------------------------------*
  230.  * Function   : fe_decode
  231.  *
  232.  * Parameters : MPPH_result:        Handle on where we put the new thing in
  233.  *                    heap
  234.  *        char *description:      If not null, the address of a 
  235.  *                     description buffer on the front end
  236.  *
  237.  * Description: Builds a new structure as described in the scratch buffer
  238.  *        If no address for the description is given then the 
  239.  *        message is assumed to be in the scratch memory already,
  240.  *        if next_byte will read in the next segment if needed.
  241.  *        By pre-initialising the global state variables of this 
  242.  *        function, this code can be used to decode objects encoded
  243.  *        by the back-end encode function (see above). encode
  244.  *        qurantees the description to fit so that no paging is
  245.  *        required (albeit discarding data in the process)
  246.  *
  247.  * Result     : int:    FAIL/SUCCESS
  248.  *---------------------------------------------------------------------------*/
  249.  
  250. #ifdef __STDC__
  251.  
  252. int fe_decode( MP_PluralHeap MPPH_result, char *description )
  253.  
  254. #else
  255.  
  256. int fe_decode( MPPH_result, description )
  257.  
  258. MP_PluralHeap MPPH_result;
  259. char *description;
  260.  
  261. #endif
  262.  
  263. {
  264.   int i;
  265.   int result_status;
  266.   plural int type;
  267.   plural int size;
  268.   plural natural tmp = NIL;
  269.   MP_PluralHeap MPPH_tmp = &tmp;
  270.   plural natural *plural general_vector;
  271. DBG_CALL("fe_decode");
  272. DBG_ARGS(fprintf(dbg,"MPPH_result = ????"));
  273.  
  274.   type        = (plural int) next_byte(description);
  275.   description = (char *)     NULL;
  276.  
  277.   if (type == NULL) {
  278.  
  279.     OA_offsets(MPPH_result) = NIL;
  280.  
  281.   }
  282.   else if (type == MP_SPECIAL) {
  283.  
  284.      OA_offsets(MPPH_result) = next_byte(description);
  285.  
  286.   }
  287.   else {
  288.  
  289.     if (type == MP_VECTOR) size = (plural int) next_byte(0);
  290.  
  291.     else                   size = 1;
  292.  
  293.     if ((result_status = mp_alloc(type, size, MPPH_result)) != FAIL) {
  294.  
  295.       switch (type) {
  296.  
  297.       case MP_CONS :
  298.  
  299.     size = 2;
  300.  
  301.       case MP_VECTOR :
  302.  
  303.     general_vector = (plural natural *plural) OA_data(MPPH_result);
  304.  
  305.     for ( i=0; i<size; i++ ) general_vector[i] = NIL;
  306.     
  307.     for ( i=0; i<size; i++ ) {
  308.  
  309.       tmp = general_vector[i];
  310.       fe_decode( MPPH_tmp, 0 );
  311.       *(general_vector + i) = tmp;
  312.     }
  313.     break;
  314.  
  315.       case INTEGER  :
  316.       case MP_FLOAT :
  317.       case MP_SYMBOL:
  318.  
  319.     for( i=0; i<sizeof(int); i++) 
  320.       *(OA_data(MPPH_result)+i) = next_byte(0);
  321.     
  322.     break;
  323.       }
  324.     }
  325.   }
  326.  
  327.   if (result_status == FAIL) {
  328.  
  329. DBG_EXIT(fprintf(dbg,"FAIL: Unable to allocate space to build objects"));
  330.     return FAIL;
  331.   }
  332.  
  333. DBG_EXIT(fprintf(dbg,"SUCCESS: As far as we can tell"));
  334.   return SUCCESS;
  335. }
  336.  
  337. /*----------------------------------------------------------------------------*
  338.  * Function   : decode
  339.  *
  340.  * Parameters : MP_PluralHeap MPPH_result:    Handle on heap loaction of
  341.  *                        resulting objects
  342.  *
  343.  * Description: Creates objects on active processors using description in
  344.  *        the scratch space. This is the sister function to decode.
  345.  *
  346.  * Result     : int:    FAIL/SUCCESS
  347.  *---------------------------------------------------------------------------*/
  348.  
  349. #ifdef __STDC__
  350.  
  351. int decode( MP_PluralHeap MPPH_result )
  352.  
  353. #else
  354.  
  355. int decode( MPPH_result )
  356.  
  357. MP_PluralHeap MPPH_result;
  358.  
  359. #endif
  360.  
  361. {
  362.   int i;
  363.   plural char *plural to_des_length = (plural char *plural) &fe_des_length;
  364.  
  365. DBG_CALL("decode");
  366. DBG_ARGS(fprintf(dbg,"MPPH_result=????"));
  367.  
  368.   fe_des_length = (plural int) scratch[0];
  369.   fe_des_index  = (plural int) 1;
  370.  
  371.   if (fe_decode(MPPH_result, NULL) == FAIL) {
  372.  
  373. DBG_FAIL(fprintf(dbg,"FAIL: Unable to allocate space??"));
  374.     return FAIL;
  375.   }
  376.  
  377. DBG_EXIT(fprintf(dbg,"SUCCESS"));
  378.   return SUCCESS;
  379. }
  380.  
  381.